package com.soccer.soccerclub.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.soccer.soccerclub.entity.*;
import com.soccer.soccerclub.entity.dto.SeatDto;
import com.soccer.soccerclub.entity.vo.SeatVO;
import com.soccer.soccerclub.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/mark")
public class MarkController {
    @Autowired
    private MarkService markService;
    @Autowired
    private PlayingFieldService playingFieldService;
    @Autowired
    private SeatService seatService;
    @Autowired
    private GameOrderService gameOrderService;
    @Autowired
    private OrderItemService orderItemService;
    /**
     * 级联列表数据
     * @return
     */
    @GetMapping("/list")
    public List<SeatDto>  list(Integer type){
        List<PlayingField> list =null;
        if (ObjectUtils.isEmpty(type)||type==2){
            list  = playingFieldService.list(new LambdaQueryWrapper<PlayingField>().ne(PlayingField::getAreaType, 8).eq(PlayingField::getStatus, 0));
        }else {
            list = playingFieldService.list(new LambdaQueryWrapper<PlayingField>().eq(PlayingField::getAreaType, 6).eq(PlayingField::getStatus, 0));
        }
        List<Seat> seatList = seatService.list();
        List<Mark> markList = markService.list();
        List<SeatDto> seatDtoList = new ArrayList<>();
        for (PlayingField playingField : list) {
            SeatDto seatDto = new SeatDto();
            seatDto.setValue(playingField.getAreaCode());
            seatDto.setLabel(playingField.getAreaCode().toString());
            seatDto.setPrice(playingField.getAreaPrice());
            List<SeatDto> seatDtoList2 = new ArrayList<>();
            for (Seat seat : seatList) {
                SeatDto seatDto2 = new SeatDto();
                seatDto2.setValue(seat.getId());
                seatDto2.setLabel(seat.getSeatNumber());
                List<SeatDto> seatDtoList3 = new ArrayList<>();
                for (Mark mark : markList) {
                    SeatDto seatDto3 = new SeatDto();
                    seatDto3.setValue(mark.getId());
                    seatDto3.setLabel(mark.getSeatMark());
                    seatDtoList3.add(seatDto3);
                }

                seatDto2.setChildren(seatDtoList3);
                seatDtoList2.add(seatDto2);
            }
            seatDto.setChildren(seatDtoList2);
            seatDtoList.add(seatDto);
        }
        return seatDtoList;
    }


    @PostMapping("/getPrice")
    public R getPrice(@RequestBody SeatVO seatVO){
        List<Integer[]> list = seatVO.getList();
        if (ObjectUtils.isEmpty(list)){
            return R.failed("还没有选定座位！！");
        }
        if(seatVO.getType()!=2){
            if (list.size()>1){
                return R.failed("学生票或军人票只能购买一张，请不要多选！！");
            }
        }
        BigDecimal price = new BigDecimal("0") ;
        for (Integer[] integers : list) {
            Integer areaCode = integers[0];
            Integer seatId = integers[1];
            Integer markId = integers[2];
            List<GameOrder> list1 = gameOrderService.list(new LambdaQueryWrapper<GameOrder>().eq(GameOrder::getGameId, seatVO.getGameId()));
            for (GameOrder order : list1) {
                List<OrderItem> orderItems = orderItemService.list(new LambdaQueryWrapper<OrderItem>().eq(OrderItem::getOrderId, order.getId()).eq(OrderItem::getAreaCode,areaCode).eq(OrderItem::getSeatId,seatId).eq(OrderItem::getMarkId,markId));
                if (ObjectUtils.isNotNull(orderItems)){
                    //该座位已被售出
                    Seat seat = seatService.getById(seatId);
                    Mark mark = markService.getById(markId);
                    return R.failed(areaCode + "区域" + seat.getSeatNumber() + mark.getSeatMark() + "座位已经售出，请选择其它座位！！");
                }
            }
            //获取区域单价
            PlayingField playingField = playingFieldService.getOne(new LambdaQueryWrapper<PlayingField>().eq(PlayingField::getAreaCode, areaCode));
            price=price.add(playingField.getAreaPrice());
        }
        List<GameOrder> gameOrders = gameOrderService.list(new LambdaQueryWrapper<GameOrder>().select(GameOrder::getGameId,GameOrder::getUserId).eq(GameOrder::getUserId, seatVO.getUserId()).groupBy(GameOrder::getGameId));
        Map<String, Object> result = new HashMap<>();
        result.put("totalPrice","￥"+price);
        result.put("discount","无折扣");
        if (gameOrders.size()>=5){
            //该用户已经买了5场比赛的票 享受9折优惠
            price= price.multiply(new BigDecimal("0.9"));
            result.put("discount","9折");
        }else if (list.size()>=10){
            //团购打九折
            price= price.multiply(new BigDecimal("0.9"));
            result.put("discount","9折");
        }else if (seatVO.getType()!=2){
            if (seatVO.getType()==3){
                result.put("discount","学生票");
            }else if (seatVO.getType()==4){
                result.put("discount","军人票");
            }
            price = new BigDecimal("30.00");
        }
        result.put("total",list.size());
        result.put("price","￥"+price);
        return R.ok(result);
    }
}

